package com.yiwei.zxp.view;

import android.app.Dialog;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.yiwei.zxp.R;
import com.yiwei.zxp.adapter.CustomParamsAdapter;
import com.yiwei.zxp.api.Api;
import com.yiwei.zxp.api.bean.CustomParamsBean;
import com.yiwei.zxp.api.bean.ParamsBean;
import com.yiwei.zxp.util.Network;
import com.yiwei.zxp.util.RxLifeUtil;
import com.yiwei.zxp.util.ToastUtil;
import com.yiwei.zxp.util.Util;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by xsooy-pc on 18-5-22.
 */

public class CustomParamsDilaog extends Dialog {


    @BindView(R.id.btn_close)
    ImageView btnClose;
    @BindView(R.id.list)
    RecyclerView list;
    @BindView(R.id.btn_submit)
    TextView btnSubmit;
    private Context context;
    private CustomParamsAdapter adapter;
    private List<ParamsBean> dataBean;
    private boolean isNeedUpdate = false;

    public CustomParamsDilaog(Context context) {
        this(context, R.style.DialogStyle);
    }

    public CustomParamsDilaog(Context context, int themeResId) {
        super(context, themeResId);
        this.context = context;
        initView();
    }

    private void initView() {
        setContentView(R.layout.dialog_params_custom);
        ButterKnife.bind(this);
        setCancelable(false);

        Window mWindow = getWindow();
        WindowManager.LayoutParams lp = mWindow.getAttributes();
        lp.width = Util.getDisplayMetrics(context).widthPixels;
        lp.gravity = Gravity.BOTTOM;
        mWindow.setAttributes(lp);
        adapter = new CustomParamsAdapter(context);
        adapter.setOnChooseListener(new CustomParamsAdapter.OnChooseListener() {
            @Override
            public void onDelete(int poi) {
                adapter.getmSource().remove(poi);
                adapter.notifyDataSetChanged();
//                adapter.update(dataBean);
            }
        });
        list.setAdapter(adapter);


    }

    public void setDataBean(List<ParamsBean> data) {
        this.dataBean = data;
        isNeedUpdate = true;
    }

    @Override
    public void show() {
        super.show();
        if (isNeedUpdate) {
            adapter.update(dataBean);
            isNeedUpdate = false;
        }
        if (adapter.getItemCount() == 0) {
            adapter.getmSource().add(new ParamsBean());
            adapter.notifyDataSetChanged();
        }
    }

    private void addData() {
        if (adapter.getItemCount() == 0) {
            adapter.getmSource().add(new ParamsBean());
            adapter.notifyDataSetChanged();
            return;
        }
        if (TextUtils.isEmpty(adapter.getmSource().get(adapter.getItemCount() - 1).name) ||
                TextUtils.isEmpty(adapter.getmSource().get(adapter.getItemCount() - 1).value)) {
            ToastUtil.makeText(context, "请补充完整后再添加");
            return;
        }
        adapter.getmSource().add(new ParamsBean());
        adapter.notifyDataSetChanged();
    }

    @OnClick({R.id.btn_add_params, R.id.btn_close, R.id.btn_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_close:
                dismiss();
                break;
            case R.id.btn_submit:
                if (TextUtils.isEmpty(adapter.getmSource().get(adapter.getItemCount() - 1).name) ||
                        TextUtils.isEmpty(adapter.getmSource().get(adapter.getItemCount() - 1).value)) {
                    ToastUtil.makeText(context, "请补充完整后再提交");
                    return;
                }
                if (onChooseListener != null) {
                    onChooseListener.onCustomParamChoose(adapter.getmSource());
                }
                dismiss();
                break;
            case R.id.btn_add_params:
                addData();
                break;
        }
    }

    private OnChooseListener onChooseListener;

    public CustomParamsDilaog setOnChooseListener(OnChooseListener onChooseListener) {
        this.onChooseListener = onChooseListener;
        return this;
    }

    public interface OnChooseListener {
        void onCustomParamChoose(List<ParamsBean> data);
    }
}
